package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.applets.entity.Menupriv;
import com.example.applets.service.impl.MenuprivServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 按钮权限 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-14
 */
@Controller
@RequestMapping("/Kshop/menupriv")
public class MenuprivController {
    @Autowired
    private MenuprivServiceImpl menuprivService;

    @RequestMapping("add")
    @ResponseBody
    //增加权限
    public AjaxResult addRight(@RequestParam("prostr") String strRight, long id) {
        //拆分字符串
        String[] str = strRight.split("-");
        //构建集合
        List<Menupriv> list = new ArrayList<Menupriv>();
        for (String s : str) {
            //如果有重复就跳过
            QueryWrapper<Menupriv> wrapper = new QueryWrapper<>();
            wrapper.eq("menuid", Long.parseLong(s));
            wrapper.eq("roleid", id);
            if (menuprivService.getOne(wrapper) != null)
                continue;
            //穿件对象加入集合
            Menupriv menupriv = new Menupriv();
            menupriv.setMenuid(Long.parseLong(s));
            menupriv.setRoleid(id);
            list.add(menupriv);
        }
        if (!menuprivService.saveBatch(list))
            return AjaxResult.success("增加权限失败!");
        return AjaxResult.success("增加权限成功!");
    }

}

